﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel;

namespace YY.WCFLib
{
    /// <summary>
    /// 传送文件信息类型
    /// </summary>
    [DataContract]
    public class TransferFileInfo
    {
        //文件名
        [DataMember]
        public string Name { get; set; }

        //文件类型
        [DataMember]
        public string Extension { get; set; }

        //文件大小
        [DataMember]
        public long Length { get; set; }

        //文件的偏移量
        [DataMember]
        public long Offset { get; set; }

        //传递的字节流
        [DataMember]
        public byte[] Data { get; set; }

        //传递的数据流
        [DataMember]
        public System.IO.Stream ByteStream { get; set; }

        //创建时间
        [DataMember]
        public DateTime CreateTime { get; set; }

        public void Dispose()
        {
            // close stream when the contract instance is disposed. this ensures that stream is closed when file download is complete, since download procedure is handled by the client and the stream must be closed on server.
            // thanks Bhuddhike! http://blogs.thinktecture.com/buddhike/archive/2007/09/06/414936.aspx
            if (ByteStream != null)
            {
                ByteStream.Close();
                ByteStream = null;
            }
        }
    }

    [MessageContract]
    public class DownloadRequest
    {
        [MessageBodyMember]
        public int ID;

        //[MessageBodyMember]
        //public string FileName;
    }

    [MessageContract]
    public class RemoteFileInfo : IDisposable
    {
        [MessageHeader(MustUnderstand = true)]
        public int ID;

        [MessageHeader(MustUnderstand = true)]
        public long Length;

        [MessageBodyMember(Order = 1)]
        public System.IO.Stream FileByteStream;

        public void Dispose()
        {
            // close stream when the contract instance is disposed. this ensures that stream is closed when file download is complete, since download procedure is handled by the client and the stream must be closed on server.
            // thanks Bhuddhike! http://blogs.thinktecture.com/buddhike/archive/2007/09/06/414936.aspx
            if (FileByteStream != null)
            {
                FileByteStream.Close();
                FileByteStream = null;
            }
        }
    }
}
